﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace prjCompras
{
    public partial class frmConsRequisicaoPlanoCompras : FormConsultaPadrao.FormConsulta
    {


        public frmConsRequisicaoPlanoCompras()
        {
            InitializeComponent();
        }

        string camposReq = @"r.Codigo, r.Data, r.Observacao, 
                             case when r.Urgente = 0 then 'Não' else 'Sim' end as Urgente,
                             case when r.Situacao = 0 then 'Não Analisada' when r.Situacao = 1 then 'Aprovada' else 'Reprovada' end as Situacao,
                             case when r.Tipo = 0 then 'Requisição' else 'Plano de Compras' end as Tipo";
        string camposMatReq = @"m.Codigo Código, m.Nome as Material, rm.Quantidade";

        private void frmConsRequisicaoPlanoCompras_Load(object sender, EventArgs e)
        {
            tlstrpbtnLimpar_Click(null, null);
            tlstrpbtnPesquisar_Click(null, null);
        }

        private void btnSair_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void chckbxUrgente_Click(object sender, EventArgs e)
        {
            if (chckbxUrgente.CheckState == CheckState.Checked)
            {
                chckbxUrgente.CheckState = CheckState.Unchecked;
            }
            else if (chckbxUrgente.CheckState == CheckState.Unchecked)
            {
                chckbxUrgente.CheckState = CheckState.Indeterminate;
            }
            else
            {
                chckbxUrgente.CheckState = CheckState.Checked;
            }
        }

        public override void tlstrpbtnPesquisar_Click(object sender, EventArgs e)
        {
            sql = "set dateformat 'dmy'; Select " + camposReq + " from Requisicao r ";
            if (chckbxData.Checked || cmbxSituacao.SelectedIndex != 3 || cmbxTipo.SelectedIndex != 2 || chckbxUrgente.CheckState != CheckState.Indeterminate)
            {
                sql += " where ";
            }
            string condicao = "";

            if (chckbxData.Checked)
            {
                if (condicao != "")
                {
                    condicao += " and ";
                }
                condicao += "r.data between '" + dtmpckerDataInicio.Value.ToString() + "' and '" + dtmpckerDataFim.Value.ToString() + "' ";
            }

            if (cmbxSituacao.SelectedIndex != 3)
            {
                if (condicao != "")
                {
                    condicao += " and ";
                }
                condicao += " Situacao = '" + cmbxSituacao.SelectedIndex.ToString() + "'";
            }
            if (cmbxTipo.SelectedIndex != 2)
            {
                if (condicao != "")
                {
                    condicao += " and ";
                }
                condicao += "  Tipo = '" + cmbxTipo.SelectedIndex.ToString() + "'";
            }
            if (chckbxUrgente.CheckState != CheckState.Indeterminate)
            {
                if (condicao != "")
                {
                    condicao += " and ";
                }
                if (chckbxUrgente.Checked)
                {
                    condicao += " Urgente = 1";
                }
                else
                {
                    condicao += " Urgente = 0";
                }
            }

            sql += condicao;
            base.tlstrpbtnPesquisar_Click(sender, e);

            dtgrdvwPagina1.Columns[0].HeaderText = "Código";
            dtgrdvwPagina1.Columns[1].HeaderText = "Data";
            dtgrdvwPagina1.Columns[2].HeaderText = "Observação";
            dtgrdvwPagina1.Columns[3].HeaderText = "Urgente";
            dtgrdvwPagina1.Columns[4].HeaderText = "Situação";
            dtgrdvwPagina1.Columns[5].HeaderText = "Tipo";
        }

        private void chckbxUrgente_Click_1(object sender, EventArgs e)
        {
            if (chckbxUrgente.CheckState == CheckState.Checked)
            {
                chckbxUrgente.CheckState = CheckState.Unchecked;
            }
            else if (chckbxUrgente.CheckState == CheckState.Unchecked)
            {
                chckbxUrgente.CheckState = CheckState.Indeterminate;
            }
            else
            {
                chckbxUrgente.CheckState = CheckState.Checked;
            }
        }

        private void chckbxData_Click(object sender, EventArgs e)
        {
            grpbxData.Enabled = chckbxData.Checked;
        }

        private void dtmpckerDataInicio_ValueChanged_1(object sender, EventArgs e)
        {
            dtmpckerDataFim.MinDate = dtmpckerDataInicio.Value;
        }

        public override void tlstrpbtnLimpar_Click(object sender, EventArgs e)
        {
            chckbxData.Checked = true;
            dtmpckerDataInicio.Value = Convert.ToDateTime("01/01/3000");
            tlstrpbtnPesquisar_Click(null, null);
            dtmpckerDataInicio.Value = System.DateTime.Now;
            dtmpckerDataFim.Value = dtmpckerDataInicio.Value;
            cmbxSituacao.SelectedIndex = 3;
            cmbxTipo.SelectedIndex = 2;
            chckbxUrgente.CheckState = CheckState.Indeterminate;
            chckbxData.Checked = false;
        }

        public override void dtgrdvwPagina1_SelectionChanged(object sender, EventArgs e)
        {
            string codReq = "";
            if (dtgrdvwPagina1.RowCount > 0)
            {
                codReq = dtgrdvwPagina1.CurrentRow.Cells[0].Value.ToString();
                string sql = @"Select " + camposMatReq + " from " + ConstTabelas.REQUISIÇÃO + " as r," + ConstTabelas.REQUISICAOMATERIAL +
                         @" as rm, " + ConstTabelas.MATERIAL + @" as m
                         where r.Codigo = rm.CodigoRequisicao 
                           and r.Codigo = '" + codReq + @"'
                           and m.Codigo = rm.CodigoMaterial";
                dtgrdvwMateriais.DataSource = Conexao.getInstance.executeQuery(sql);
            }
            else
            {
                string sql = @"Select null Código, null Material, null Quantidade where 1=2";
                dtgrdvwMateriais.DataSource = Conexao.getInstance.executeQuery(sql);
            }
            base.dtgrdvwPagina1_SelectionChanged(sender, e);
        }

    }
}
